import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
 class Main1 {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            String n1 = s.substring(0, 3);
            float n = Float.parseFloat(n1);


            char status = s.charAt(3);


            float sum = 0;

            if (status == 'y') {

                if (n <= 1) {
                    sum = 25;
                    // System.out.print(sum);
                    return;
                } else {

                    int add = (int) (n * 10);


                    if ((int) (n * 10) % 10 != 0) {
                        add /= 10;
                    } else {
                        add = add / 10 - 1;

                    }
                    sum = 25 + add;

                    System.out.print(sum);
                    return;
                }

            } else {
                if (n <= 1) {
                    sum = 20;
                    System.out.print(sum);
                    return;
                } else {
                    int add = ((int) n * 10);
                    if ((int) (n * 10) % 10 != 0) {
                        add /= 10;
                    } else {
                        add = add / 10 - 1;

                    }
                    sum = 20 + add;

                    System.out.print(sum);
                    return;
                }

            }


        }


    }
}





// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Main {
    public static int isContain(String[] strs, String str1, String str2) {
        int n1 = -1;
        int n2 = -1;
        int n = -1;
        for (int i = 0; i < strs.length; i++) {
            if (n1 != -1 && n2 != -1) {
                n = n1 - n2;
                if (n < 0) {
                    n = -n;
                }
            }



            if (strs[i].equals(str1)) {

                if(n!=-1){
                    int newn=i-n2;
                    if(newn<0) newn=-newn;
                    if(newn<n){
                        n=newn;

                    }else{
                        continue;
                    }
                }
                n1 = i;


            }

            if (strs[i].equals(str2)) {
                if(n!=-1){
                    int newn=i-n1;
                    if(newn<0) newn=-newn;
                    if(newn<n){
                        n=newn;

                    }else{
                        continue;
                    }
                }
                n2 = i;
            }

        }


        return n;




    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();

            String str1 = in.next();
            String str2 = in.next();



            String[] strs = new String[n];

            for (int i = 0; i < n; i++) {
                strs[i] = in.next();
            }


            System.out.println(isContain(strs, str1, str2));
        }
    }
}







// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            if(a==0){
                return;
            }

            int[] cost=new int[a];
            for(int i=0;i<a;i++){
                cost[i]=in.nextInt();
            }

            int start=0;
            if(cost[0]>cost[1]) {
                start=1;
            }
            int sum=0;

            if(a==3){

                if(cost[0]+cost[2]>=cost[1]){
                    sum+=cost[1];
                    System.out.println(sum);
                    return;

                }

            }else if(a<3){
                if(a==1){
                    System.out.println(cost[0]);
                }else{
                    sum=cost[1]>cost[0]?cost[0]:cost[1];
                    System.out.println(sum);
                    return;
                }
            }




            while(start<a-2){
                sum+=cost[start];
                if(cost[start+1]>=cost[start+2]){
                    start+=2;
                }else{
                    start+=1;
                }
            }

            if(a-start==3){
                if(cost[start]+cost[start+2]>cost[start+1]+cost[start]){
                    sum+=(cost[start+1]+cost[start]);
                }else sum+=(cost[start+2]+cost[start]);
            }else{
                sum+=cost[start];
            }



            System.out.println(sum);
        }
    }
}